package com.ruoyi.swzl.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.email.EmailUtil;
import com.ruoyi.common.utils.email.Mail;
import com.ruoyi.swzl.domain.Goods;
import com.ruoyi.swzl.mapper.GoodsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.swzl.mapper.ReceiveMapper;
import com.ruoyi.swzl.domain.Receive;
import com.ruoyi.swzl.service.IReceiveService;

import javax.annotation.Resource;

/**
 * 领取登记Service业务层处理
 * 
 * @author k
 * @date 2025-03-29
 */
@Service
public class ReceiveServiceImpl implements IReceiveService 
{
    @Resource
    private ReceiveMapper receiveMapper;

    @Resource
    private GoodsMapper goodsMapper;

    @Resource
    private EmailUtil emailUtil;

    /**
     * 查询领取登记
     * 
     * @param id 领取登记主键
     * @return 领取登记
     */
    @Override
    public Receive selectReceiveById(Long id)
    {
        return receiveMapper.selectReceiveById(id);
    }

    /**
     * 查询领取登记列表
     * 
     * @param receive 领取登记
     * @return 领取登记
     */
    @Override
    public List<Receive> selectReceiveList(Receive receive)
    {
        return receiveMapper.selectReceiveList(receive);
    }

    /**
     * 新增领取登记
     * 
     * @param receive 领取登记
     * @return 结果
     */
    @Override
    public int insertReceive(Receive receive)
    {
        receive.setCreateTime(DateUtils.getNowDate());
        Goods goods = goodsMapper.selectGoodsById(receive.getGoodsId());
        goods.setDelFlag("2");
        String receiveEmail = goods.getEmail();
        Mail mail = new Mail();
        mail.setRecipient(receiveEmail);
        mail.setContent("亲爱的用户：您好！\n" +
                "\n" + "    您收到这封电子邮件是因为您之前在失物招领网站上登记的拾取物品已经被失主认领啦！感谢您给予的帮助！\n" );
        emailUtil.sendSimpleMail(mail);
        goodsMapper.updateGoods(goods);
        return receiveMapper.insertReceive(receive);
    }

    /**
     * 修改领取登记
     * 
     * @param receive 领取登记
     * @return 结果
     */
    @Override
    public int updateReceive(Receive receive)
    {
        receive.setUpdateTime(DateUtils.getNowDate());
        return receiveMapper.updateReceive(receive);
    }

    /**
     * 批量删除领取登记
     * 
     * @param ids 需要删除的领取登记主键
     * @return 结果
     */
    @Override
    public int deleteReceiveByIds(Long[] ids)
    {
        return receiveMapper.deleteReceiveByIds(ids);
    }

    /**
     * 删除领取登记信息
     * 
     * @param id 领取登记主键
     * @return 结果
     */
    @Override
    public int deleteReceiveById(Long id)
    {
        return receiveMapper.deleteReceiveById(id);
    }
}
